﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CommonLibrary
{
    public  class MonitorWindowsRegDemo
    {
        private MonitorRegEditHelper m_MonitorWindowsReg;
        public  void RunDemo()
        {
            // @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\ImmersiveShell" Key：TabletMode    桌面模式/平板模式
            Microsoft.Win32.RegistryKey _Key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\ImmersiveShell");
            Console.WriteLine($"Current Value: {_Key.GetValue("TabletMode")}");
            m_MonitorWindowsReg = new MonitorRegEditHelper(_Key);
            m_MonitorWindowsReg.UpReg += M_MonitorWindowsReg_UpReg;
            m_MonitorWindowsReg.Star();
        }

        private void M_MonitorWindowsReg_UpReg(string OldText, object OldValue, string NewText, object NewValue)
        {
            object Old = OldValue;
            object New = NewValue;
            if (Old == null) Old = "";
            if (New == null) New = "";
            string outstring = $"Monitor Reg OldValue={Old}   NewValue={New}";
            Console.WriteLine(outstring);
            //MessageBox.Show(OldText + ":" + Old.ToString(), NewText + ":" + New.ToString());
        }

        private void UpdateModeFromRegistry()
        {
            var tabletMode = (int)Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\ImmersiveShell", "TabletMode", 0);
            if (tabletMode == 1)
            {
                Console.Write(@"Tablet mode is enabled");
            }
            else
            {
                Console.Write(@"Tablet mode is disabled");
            }
        }

        public void Close()
        {
            m_MonitorWindowsReg.Stop();
        }
    }
}
